﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HTTT.Utilities;
using System.Web.Security;

namespace HTTT.admin.htuser
{
    public partial class ResetPwd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
                CheckPermission();
            }
        }

        private void LoadData()
        {
            string username = Request.QueryString["ID"];
            LoadDetail(username);
        }

        private void LoadDetail(string username)
        {
            txtUsername.Text = username;
        }

        
        private void CheckPermission()
        {
            if (Page.User.IsInRole(Constants.ROLE_SUPER_ADMINISTRATOR))
            {
                //
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "Message", Common.FormatMsgThenRedirect("Bạn không có quyền", "/admin/AminIndex.aspx"), true);
            }
        }


        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            string username = txtUsername.Text;
            if (txtPwd.Text != txtPwdAgain.Text)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "Redirect", Common.FormatAlertMsg("Mật khẩu không trùng nhau"), true);
            }
            else
            {
                MembershipUser usr = Membership.GetUser(username);
                string resetPwd = usr.ResetPassword();
                bool result = usr.ChangePassword(resetPwd, txtPwd.Text.Trim());
                if (result)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "Redirect", Common.FormatMsgThenRedirect("Đặt lại mật khẩu thành công", "UserList.aspx"), true);
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "Redirect", Common.FormatAlertMsg("Không thành công"), true);
                }
            }
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("UserList.aspx");
        }
    }
}